home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / src / net / bind-contrib.tar.gz / bind-contrib.tar / contrib / tic / gendns next >
Text File  |  1996-10-25  |  5KB  |  193 lines

  1. #!/bin/sh
  2. # @(#) gendns 1.15 92/08/31 @(#)
  3. # generate the DNS database files from generic database files
  4. #
  5. # files used
  6. #    hosts.main
  7. #    hosts.aliases
  8. #    hosts.mx
  9. #    hosts.ns
  10. #    hosts.wks
  11. #
  12. # this script assumes that a subdomain is always delegated to
  13. # another nameserver and hosts for the subdomain except for the
  14. # glue records do not exist in the base host database files
  15.  
  16. # Copyright (c) 1992 by Texas Internet Consulting
  17. # This code may be freely copied and used so long as this
  18. # copyright notice is attached.  This code may not be sold
  19. # without the express written permission of Texas Internet Consulting.
  20. # Texas Internet Consulting makes no warranty as to the correctness
  21. # nor the applicability of this code for any purpose.
  22.  
  23. # make the forward database files
  24.  
  25. makeforw() {
  26.  
  27.     # extract host name and IP address from hosts.main
  28.     # and make A records out of it,
  29.     # but only for hosts within this domain
  30.     # add NS and A records for the servers
  31.     # add HINFO from hardware and os
  32.  
  33.     ( readinfo host ip hard os <hosts.main | awk -F'    ' '
  34.     BEGIN {
  35.         nservers = split("'"$servers"'", servers, " ")
  36.         for (i=1; i<=nservers; i++) {
  37.             printf("'$domain'. IN NS %s.\n", servers[i])
  38.         }
  39.     }
  40.     {
  41.         for (i=1; i<=nservers; i++) {
  42.             if ($1 == servers[i]) {
  43.                 printf("%s. IN A %s\n", $1, $2)
  44.                 if ($3 != "X") {
  45.                     printf("%s. IN HINFO %s %s\n", $1, $3, $4)
  46.                 }
  47.                 next
  48.             }
  49.         }
  50.         if ($1 ~ /'$escape_domain'$/) {
  51.              printf("%s. IN A %s\n", $1, $2)
  52.             if ($3 != "X") {
  53.                 printf("%s. IN HINFO %s %s\n", $1, $3, $4)
  54.             }
  55.         }
  56.     }'
  57.  
  58.     # make cname records out of the aliases
  59.     # again only for this domain
  60.  
  61.     readinfo alias host <hosts.cname | awk -F'    ' '{
  62.         if ($1 ~ /'$escape_domain'$/)
  63.             printf("%s. IN CNAME %s.\n", $1, $2)
  64.     }'
  65.  
  66.     # and the MX records the same way
  67.  
  68.     readinfo domain priority host <hosts.mx | awk -F'    ' '{
  69.         if ($1 ~ /'$escape_domain'$/)
  70.             printf("%s. IN MX %s %s.\n", $1, $2, $3)
  71.     }'
  72.  
  73.     # and WKS records
  74.     # sort them and concatenate the application protocol mnemonics
  75.  
  76.     readinfo host ip proto wks <hosts.wks | sort | awk -F'    ' '{
  77.         if ($1 ~ /'$escape_domain'$/) {
  78.             if ($1 != host || $2 != ip || $3 != proto) {
  79.                 if (host != "") {
  80.                     printf("%s. IN WKS %s %s %s\n", host, ip, proto, wks)
  81.                     wks = ""
  82.                 }
  83.             }
  84.             host = $1
  85.             ip = $2
  86.             proto = $3
  87.             wks = wks " " $4
  88.         }
  89.     }
  90.     END {
  91.         printf("%s. IN WKS %s %s %s\n", host, ip, proto, wks)
  92.     }' )
  93. }
  94.  
  95. makerev() {
  96.  
  97.     # extract host name and IP address from hosts.main
  98.     # and make inverse PTR records out of it
  99.     # but only for IP addresses which match the IP addresses in this domain
  100.  
  101.     readinfo host ip <hosts.main | awk -F'    ' '
  102.     BEGIN {
  103.         nrev = split("'$unreverse'", unreverse, ".")
  104.         nservers = split("'"$servers"'", servers, " ")
  105.         for (i=1; i<=nservers; i++) {
  106.             printf("'$domain'. IN NS %s.\n", servers[i])
  107.         }
  108.     }
  109.     {
  110.         # check for servers and output glue A records
  111.         for (i=1; i<=nservers; i++) {
  112.             if ($1 == servers[i]) {
  113.                 printf("%s. IN A %s\n", $1, $2)
  114.             }
  115.         }
  116.         n = split($2, ipparts, ".")
  117.         for (i=1; i<=nrev; i++) {
  118.             if (ipparts[i] != unreverse[i])
  119.                 break
  120.         }
  121.         if (i <= nrev)
  122.             next
  123.         for(; i<=n; i++)
  124.             printf("%s.", ipparts[i])
  125.         printf("%s. IN PTR %s.\n", "'$domain'", $1)
  126.     }'
  127. }
  128.  
  129. SERIAL=serial
  130.  
  131. serial=`cat $SERIAL`
  132. dateserial=`date +%y%m%d`
  133.  
  134. # read SOA info for each domain
  135. readinfo domain server contact refresh retry expire min <hosts.soa |\
  136. while read domain server contact refresh retry expire min; do
  137.  
  138.     # get the domain name with escaped "."
  139.  
  140.     escape_domain=`echo $domain | awk -F. '{
  141.         for (i=1; i<NF; i++) {
  142.             printf("%s", $i)
  143.             printf("\\.")
  144.         }
  145.         printf("%s", $NF)
  146.     }'`
  147.  
  148.     # get servers for this domain
  149.  
  150.     servers=`readinfo domain server <hosts.ns | awk -F'    ' '{
  151.         if ($1 ~ /'$escape_domain'$/)
  152.             printf("%s ", $2)
  153.     }'`
  154.  
  155.     case $domain in
  156.     *.in-addr.arpa)
  157.         rev=true
  158.         # get the filename fo reverse domains    
  159.         # unreverse the domain name for matching IP addresses
  160.         unreverse=`echo $domain | awk -F. '{
  161.             for (i=NF-2; i>1; i--)
  162.                 printf("%s.", $i)
  163.             printf("%s", $1)
  164.         }'`
  165.         filename=f.$unreverse ;;
  166.  
  167.     *)
  168.         rev=
  169.         filename=$domain ;;
  170.     esac
  171.  
  172.     # print the header and SOA record
  173.  
  174.     ( echo "; $serial"
  175.     echo '$ORIGIN'" ${domain}."
  176.     echo "@ SOA ${server}. ${contact}. ( $dateserial $refresh $retry $expire $min )"
  177.  
  178.     # scan the input file and extract info depending on whether this
  179.     # is a forward or reverse domain file
  180.  
  181.     if [ $rev ]; then
  182.         makerev
  183.     else
  184.         makeforw
  185.     fi |\
  186.  
  187.     # get rid of extraneous domain info
  188.  
  189.     sed -e 's/\(.*\)\(\.'${escape_domain}'\.\)/\1/g' \
  190.     -e 's/\(.*\)\(\.'${escape_domain}'\. \)/\1 /g' \
  191.          -e 's/^'${escape_domain}'\./@/g' ) >$filename
  192. done
  193.